package de.fhffm.scs.simulator.gui.composites;

import org.csstudio.swt.xygraph.dataprovider.IDataProvider;
import org.csstudio.swt.xygraph.figures.Trace;
import org.csstudio.swt.xygraph.figures.Trace.TraceType;
import org.csstudio.swt.xygraph.figures.XYGraph;
import org.csstudio.swt.xygraph.linearscale.Range;
import org.csstudio.swt.xygraph.util.XYGraphMediaFactory;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

import de.fhffm.scs.simulator.data.BloodSugarDB;
import de.fhffm.scs.simulator.data.BloodSugarLevel;
import de.fhffm.scs.simulator.regulator.SugarLevelController;

public class RegulationVisulisation extends Composite {

	SugarLevelController controller;
	private LightweightSystem lws;
	private BloodSugarDB bloodSugarDB;
	private Trace trace;
	private XYGraph xyGraph;

	public RegulationVisulisation(SugarLevelController controller,
			Composite parent, int style) {
		super(parent, SWT.NONE);

		this.controller = controller;

		setLayout(new GridLayout(1, false));

		Group grpBloodsugarHistory = new Group(this, SWT.NONE);
		grpBloodsugarHistory.setText("Bloodsugar History");
		grpBloodsugarHistory.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				true, true, 1, 1));
		grpBloodsugarHistory.setBounds(0, 0, 78, 78);
		grpBloodsugarHistory.setLayout(new GridLayout(1, false));

		Canvas canvas = new Canvas(grpBloodsugarHistory, SWT.NONE);
		canvas.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1,
				1));
		GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gridData.widthHint = 300;
		canvas.setLayoutData(gridData);

		lws = new LightweightSystem(canvas);

		bloodSugarDB = (BloodSugarDB) controller.getBloodSugarDB();
		if(bloodSugarDB.getSize() == 0){
			bloodSugarDB.addBloodSugarLevel(new BloodSugarLevel(0, System.currentTimeMillis()));
		}
		
		addBloodSugarLevelGraph(bloodSugarDB);

	}

	private void addBloodSugarLevelGraph(final IDataProvider bslDB) {
		// use LightweightSystem to create the bridge between SWT and draw2D

		xyGraph= new XYGraph();
		xyGraph.setTitle("Recorded Blood Sugar Levels");
		// set it as the content of LightwightSystem
		lws.setContents(xyGraph);

		// Configure XYGraph
		xyGraph.primaryXAxis.setTitle("Time");
		xyGraph.primaryYAxis.setTitle("Bloodsugar Level");
		xyGraph.primaryXAxis.setRange(new Range(0, 300));
		xyGraph.primaryXAxis.setDateEnabled(true);
		xyGraph.primaryYAxis.setAutoScale(true);
		xyGraph.primaryXAxis.setAutoScale(true);
		xyGraph.primaryXAxis.setShowMajorGrid(true);
		xyGraph.primaryYAxis.setShowMajorGrid(true);
		xyGraph.primaryXAxis.setAutoScaleThreshold(0);

		trace = new Trace("Blood Sugar Level", xyGraph.primaryXAxis,
				xyGraph.primaryYAxis, bslDB);

		// set trace property
		trace.setPointSize(6);
		trace.setAreaAlpha(100);
		trace.setTraceType(TraceType.AREA);
		trace.setTraceColor(XYGraphMediaFactory.getInstance().getColor(
				XYGraphMediaFactory.COLOR_RED));

		// add the trace to xyGraph
		xyGraph.addTrace(trace);
	}

	public void update() {
		trace.dataChanged(bloodSugarDB);
		xyGraph.performAutoScale();
	}
}
